<html>
<head><meta charset="utf-8"><title>Evaluate format string in proc-macro #80337 · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro.20.2380337.html">Evaluate format string in proc-macro #80337</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="220804105"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Evaluate%20format%20string%20in%20proc-macro%20%2380337/near/220804105" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro.20.2380337.html#220804105">(Dec 23 2020 at 16:35)</a>:</h4>
<p>So I know this is cursed, but I don't see another approach :( Queries in the compiler have a <a href="https://github.com/rust-lang/rust/blob/3ffea60dd5a2260004cc4f487401ae7c7db1aa0e/compiler/rustc_middle/src/query/mod.rs#L40"><code>desc</code>ription</a> of what the query does:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="n">rustc_queries</span><span class="o">!</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">Other</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">query</span><span class="w"> </span><span class="n">trigger_delay_span_bug</span><span class="p">(</span><span class="n">key</span>: <span class="nc">DefId</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="n">desc</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="s">"trigger a delay span bug"</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>This is processed by the <a href="https://github.com/rust-lang/rust/blob/3ffea60dd5a2260004cc4f487401ae7c7db1aa0e/compiler/rustc_macros/src/query.rs#L65"><code>rustc_queries</code></a> proc-macro.</p>
<p>What I want to do is use that description as a doc-comment if the query doesn't already have one. I have this code so far:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">                </span><span class="k">if</span><span class="w"> </span><span class="n">query</span><span class="p">.</span><span class="n">doc_comments</span><span class="p">.</span><span class="n">is_empty</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                    </span><span class="c1">// FIXME: replace `{}` with `_` somehow</span>
<span class="w">                    </span><span class="kd">let</span><span class="w"> </span><span class="n">format_str</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">list</span><span class="p">.</span><span class="n">first</span><span class="p">().</span><span class="n">unwrap</span><span class="p">();</span><span class="w"></span>
<span class="w">                    </span><span class="kd">let</span><span class="w"> </span><span class="n">comment</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">parse_quote</span><span class="o">!</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                        </span><span class="cp">#[doc = #format_str]</span><span class="w"></span>
<span class="w">                    </span><span class="p">};</span><span class="w"></span>
<span class="w">                    </span><span class="n">query</span><span class="p">.</span><span class="n">doc_comments</span><span class="p">.</span><span class="n">push</span><span class="p">(</span><span class="n">comment</span><span class="p">);</span><span class="w"></span>
<span class="w">                </span><span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>The problem is that this renders format strings directly, without substituting any of the <code>{}</code> specifiers. The data to do that usefully isn't available at compile time, so I'm fine with just replacing them with <code>_</code> instead. But I'm not sure how to do that :/ is there a way to introspect format strings so I don't have to parse them myself? Or do I need to do a blanket <code>str.replace("{}", "_")</code> (however that works with <code>syn</code>)?</p>



<a name="220804178"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Evaluate%20format%20string%20in%20proc-macro%20%2380337/near/220804178" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro.20.2380337.html#220804178">(Dec 23 2020 at 16:35)</a>:</h4>
<p>ideally this would even replace <code>{}</code> with the stringified expr, but that's a stretch</p>



<a name="220804253"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Evaluate%20format%20string%20in%20proc-macro%20%2380337/near/220804253" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro.20.2380337.html#220804253">(Dec 23 2020 at 16:36)</a>:</h4>
<p>(so for <code>desc { |tcx| "finding projection type inside predicates of `{}`", tcx.def_path_str(key.0) }</code> it would display <code>finding projection type inside predicates of `tcx.def_path_str(key.0)` </code>)</p>



<a name="220804357"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Evaluate%20format%20string%20in%20proc-macro%20%2380337/near/220804357" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro.20.2380337.html#220804357">(Dec 23 2020 at 16:37)</a>:</h4>
<p><span class="user-mention" data-user-id="158561">@Daniel Henry-Mantilla</span> do you happen to have ideas? ;)</p>



<a name="220804704"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Evaluate%20format%20string%20in%20proc-macro%20%2380337/near/220804704" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro.20.2380337.html#220804704">(Dec 23 2020 at 16:41)</a>:</h4>
<p>I think there is a library out there which does it for you, but you could also just leave {} in, right?</p>



<a name="220804720"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Evaluate%20format%20string%20in%20proc-macro%20%2380337/near/220804720" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro.20.2380337.html#220804720">(Dec 23 2020 at 16:41)</a>:</h4>
<p>yeah, it just looks ugly</p>



<a name="220804729"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Evaluate%20format%20string%20in%20proc-macro%20%2380337/near/220804729" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro.20.2380337.html#220804729">(Dec 23 2020 at 16:42)</a>:</h4>
<p>(by which I mean a library implementing rustc/std's formatting machinery at runtime rather than compile time)</p>



<a name="220804769"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Evaluate%20format%20string%20in%20proc-macro%20%2380337/near/220804769" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro.20.2380337.html#220804769">(Dec 23 2020 at 16:42)</a>:</h4>
<p><a href="/user_uploads/4715/q_ZtHNxDZczhz-SMhJY7UXgR/image.png">image.png</a></p>
<div class="message_inline_image"><a href="/user_uploads/4715/q_ZtHNxDZczhz-SMhJY7UXgR/image.png" title="image.png"><img src="/user_uploads/4715/q_ZtHNxDZczhz-SMhJY7UXgR/image.png"></a></div>



<a name="220804841"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Evaluate%20format%20string%20in%20proc-macro%20%2380337/near/220804841" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro.20.2380337.html#220804841">(Dec 23 2020 at 16:43)</a>:</h4>
<p>and for some queries it's much less clear than the format string<br>
<a href="/user_uploads/4715/PBCZhQZN7Q2qhsE46yi9sBHr/image.png">image.png</a> </p>
<div class="message_inline_image"><a href="/user_uploads/4715/PBCZhQZN7Q2qhsE46yi9sBHr/image.png" title="image.png"><img src="/user_uploads/4715/PBCZhQZN7Q2qhsE46yi9sBHr/image.png"></a></div><div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">            </span><span class="n">desc</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="o">|</span><span class="n">tcx</span><span class="o">|</span><span class="w"> </span><span class="s">"processing {}`{}`"</span><span class="p">,</span><span class="w"></span>
<span class="w">                </span><span class="k">if</span><span class="w"> </span><span class="n">key</span><span class="p">.</span><span class="n">const_param_did</span><span class="p">.</span><span class="n">is_some</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="s">"the const argument "</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="s">""</span><span class="w"> </span><span class="p">},</span><span class="w"></span>
<span class="w">                </span><span class="n">tcx</span><span class="p">.</span><span class="n">def_path_str</span><span class="p">(</span><span class="n">key</span><span class="p">.</span><span class="n">did</span><span class="p">.</span><span class="n">to_def_id</span><span class="p">()),</span><span class="w"></span>
<span class="w">            </span><span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="220804972"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Evaluate%20format%20string%20in%20proc-macro%20%2380337/near/220804972" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro.20.2380337.html#220804972">(Dec 23 2020 at 16:45)</a>:</h4>
<p>let me try runtime_fmt</p>



<a name="220805464"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Evaluate%20format%20string%20in%20proc-macro%20%2380337/near/220805464" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro.20.2380337.html#220805464">(Dec 23 2020 at 16:50)</a>:</h4>
<p>follow-up question: how do I stringify an arbitrary <code>Expr</code>?</p>



<a name="220805536"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Evaluate%20format%20string%20in%20proc-macro%20%2380337/near/220805536" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro.20.2380337.html#220805536">(Dec 23 2020 at 16:51)</a>:</h4>
<p><code>TokenStream::Display</code> looks about right</p>



<a name="220805971"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Evaluate%20format%20string%20in%20proc-macro%20%2380337/near/220805971" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro.20.2380337.html#220805971">(Dec 23 2020 at 16:56)</a>:</h4>
<p>hmm runtime_fmt doesn't work because it takes a variable number of arguments instead of a slice :(</p>



<a name="220806454"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Evaluate%20format%20string%20in%20proc-macro%20%2380337/near/220806454" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro.20.2380337.html#220806454">(Dec 23 2020 at 17:01)</a>:</h4>
<p>ok it does actually allow this, just not using the macro which is fine</p>



<a name="220806465"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Evaluate%20format%20string%20in%20proc-macro%20%2380337/near/220806465" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro.20.2380337.html#220806465">(Dec 23 2020 at 17:01)</a>:</h4>
<p>how do I turn a <code>String</code> into a <code>syn::Expr</code>?</p>



<a name="220806482"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Evaluate%20format%20string%20in%20proc-macro%20%2380337/near/220806482" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro.20.2380337.html#220806482">(Dec 23 2020 at 17:01)</a>:</h4>
<p>I don't want to parse the string, just use it as a string literal</p>



<a name="220806617"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Evaluate%20format%20string%20in%20proc-macro%20%2380337/near/220806617" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro.20.2380337.html#220806617">(Dec 23 2020 at 17:03)</a>:</h4>
<p>oh booo runtime_fmt doesn't actually work with current nightly :(</p>



<a name="220806625"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Evaluate%20format%20string%20in%20proc-macro%20%2380337/near/220806625" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro.20.2380337.html#220806625">(Dec 23 2020 at 17:03)</a>:</h4>
<p>ok I'm giving up on this for now</p>



<a name="220806875"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Evaluate%20format%20string%20in%20proc-macro%20%2380337/near/220806875" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro.20.2380337.html#220806875">(Dec 23 2020 at 17:05)</a>:</h4>
<p><span class="user-mention silent" data-user-id="232545">Joshua Nelson</span> <a href="#narrow/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro/near/220804178">said</a>:</p>
<blockquote>
<p>ideally this would even replace <code>{}</code> with the stringified expr, but that's a stretch</p>
</blockquote>
<p>I like this idea a lot!</p>
<p>Give me a reference to the PR / branch, and I'll see what I can do <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span></p>



<a name="220807043"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Evaluate%20format%20string%20in%20proc-macro%20%2380337/near/220807043" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro.20.2380337.html#220807043">(Dec 23 2020 at 17:07)</a>:</h4>
<p><a href="https://github.com/jyn514/rust/compare/proc-macro-fmt">https://github.com/jyn514/rust/compare/proc-macro-fmt</a></p>



<a name="220807198"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Evaluate%20format%20string%20in%20proc-macro%20%2380337/near/220807198" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro.20.2380337.html#220807198">(Dec 23 2020 at 17:08)</a>:</h4>
<p>the build process for testing this is kind of awful:</p>
<ol>
<li>build the docs (<code>x.py doc --stage 1 compiler/rustc</code>) with a commit that works, such as <a href="https://github.com/jyn514/rust/commit/f2e1f34552513b3703d6193cfd470f899850368c">https://github.com/jyn514/rust/commit/f2e1f34552513b3703d6193cfd470f899850368c</a>. You'll need to set <code>compiler-docs = true</code> in config.toml.</li>
<li>check out the broken commit</li>
<li><code>x.py doc --stage 1 compiler/rustc --keep-stage 0 --keep-stage 1</code></li>
</ol>



<a name="220807218"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Evaluate%20format%20string%20in%20proc-macro%20%2380337/near/220807218" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro.20.2380337.html#220807218">(Dec 23 2020 at 17:08)</a>:</h4>
<p>it may be easier to get this working out of tree first, I don't think anything depends on rustc specifically</p>



<a name="220807754"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Evaluate%20format%20string%20in%20proc-macro%20%2380337/near/220807754" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro.20.2380337.html#220807754">(Dec 23 2020 at 17:14)</a>:</h4>
<p>and the simple version that doesn't replace <code>{}</code> is <a href="https://github.com/rust-lang/rust/pull/80337">https://github.com/rust-lang/rust/pull/80337</a></p>



<a name="220808243"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Evaluate%20format%20string%20in%20proc-macro%20%2380337/near/220808243" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro.20.2380337.html#220808243">(Dec 23 2020 at 17:19)</a>:</h4>
<p>Hmm actually this would be a lot easier if I only handle <code>{}</code> and not the full format_args syntax - I can just write that myself without a library</p>



<a name="220808280"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Evaluate%20format%20string%20in%20proc-macro%20%2380337/near/220808280" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro.20.2380337.html#220808280">(Dec 23 2020 at 17:19)</a>:</h4>
<p>These show up in cycle errors so none of them should be using {:?}</p>



<a name="220809322"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Evaluate%20format%20string%20in%20proc-macro%20%2380337/near/220809322" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro.20.2380337.html#220809322">(Dec 23 2020 at 17:30)</a>:</h4>
<p><span class="user-mention silent" data-user-id="232545">Joshua Nelson</span> <a href="#narrow/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro/near/220808243">said</a>:</p>
<blockquote>
<p>Hmm actually this would be a lot easier if I only handle <code>{}</code> and not the full format_args syntax - I can just write that myself without a library</p>
</blockquote>
<p>I was thinking of that too, I don't think exotic formatting will be encountered</p>



<a name="220809455"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Evaluate%20format%20string%20in%20proc-macro%20%2380337/near/220809455" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro.20.2380337.html#220809455">(Dec 23 2020 at 17:32)</a>:</h4>
<p>nice, I'll try that after lunch :)</p>



<a name="220879622"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Evaluate%20format%20string%20in%20proc-macro%20%2380337/near/220879622" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro.20.2380337.html#220879622">(Dec 24 2020 at 16:44)</a>:</h4>
<p>Managed to get the following: <a href="/user_uploads/4715/SW2YahCkAGZZ5-gcRollMoMV/Screen-Shot-2020-12-24-at-17.44.12.png">Screen-Shot-2020-12-24-at-17.44.12.png</a></p>
<div class="message_inline_image"><a href="/user_uploads/4715/SW2YahCkAGZZ5-gcRollMoMV/Screen-Shot-2020-12-24-at-17.44.12.png" title="Screen-Shot-2020-12-24-at-17.44.12.png"><img src="/user_uploads/4715/SW2YahCkAGZZ5-gcRollMoMV/Screen-Shot-2020-12-24-at-17.44.12.png"></a></div>



<a name="220879731"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Evaluate%20format%20string%20in%20proc-macro%20%2380337/near/220879731" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro.20.2380337.html#220879731">(Dec 24 2020 at 16:47)</a>:</h4>
<p>oooh that's nice :D</p>



<a name="220879740"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Evaluate%20format%20string%20in%20proc-macro%20%2380337/near/220879740" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro.20.2380337.html#220879740">(Dec 24 2020 at 16:47)</a>:</h4>
<p>the whitespace is a little funky but that's probably because you have token trees and not the actual code to work with</p>



<a name="220879800"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Evaluate%20format%20string%20in%20proc-macro%20%2380337/near/220879800" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro.20.2380337.html#220879800">(Dec 24 2020 at 16:48)</a>:</h4>
<p>Yep, that was expected. I'm trying a basic <code>" . " -&gt; "."</code> replacement: empirically it provides great improvements at minor cost <span aria-label="big smile" class="emoji emoji-1f604" role="img" title="big smile">:big_smile:</span></p>



<a name="220880447"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Evaluate%20format%20string%20in%20proc-macro%20%2380337/near/220880447" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro.20.2380337.html#220880447">(Dec 24 2020 at 17:01)</a>:</h4>
<p>Looking pretty good; the one at the bottom is one of the weirdest ones I have encountered:</p>
<p><a href="/user_uploads/4715/oU4roS2n-_6xFkWolnem0lZq/Screen-Shot-2020-12-24-at-18.00.33.png">Screen-Shot-2020-12-24-at-18.00.33.png</a></p>
<div class="message_inline_image"><a href="/user_uploads/4715/oU4roS2n-_6xFkWolnem0lZq/Screen-Shot-2020-12-24-at-18.00.33.png" title="Screen-Shot-2020-12-24-at-18.00.33.png"><img src="/user_uploads/4715/oU4roS2n-_6xFkWolnem0lZq/Screen-Shot-2020-12-24-at-18.00.33.png"></a></div>



<a name="220880545"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Evaluate%20format%20string%20in%20proc-macro%20%2380337/near/220880545" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro.20.2380337.html#220880545">(Dec 24 2020 at 17:02)</a>:</h4>
<p>that's awesome!</p>



<a name="220880549"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Evaluate%20format%20string%20in%20proc-macro%20%2380337/near/220880549" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro.20.2380337.html#220880549">(Dec 24 2020 at 17:02)</a>:</h4>
<p>wow, that looks really good :D</p>



<a name="220880587"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Evaluate%20format%20string%20in%20proc-macro%20%2380337/near/220880587" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro.20.2380337.html#220880587">(Dec 24 2020 at 17:03)</a>:</h4>
<p><span class="user-mention" data-user-id="124288">@oli</span> what do you think? about <code>mir_promoted</code></p>



<a name="220881502"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Evaluate%20format%20string%20in%20proc-macro%20%2380337/near/220881502" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro.20.2380337.html#220881502">(Dec 24 2020 at 17:21)</a>:</h4>
<p>I'm a bit lazy to make a PR to your PR based off the right commit; here are the changes I applied to (<code>f2e1f3…</code>):</p>
<div class="codehilite" data-code-language="Diff"><pre><span></span><code><span class="gh">diff --git a/compiler/rustc_macros/src/query.rs b/compiler/rustc_macros/src/query.rs</span>
<span class="gh">index 6f9b79a0465..901ea261794 100644</span>
<span class="gd">--- a/compiler/rustc_macros/src/query.rs</span>
<span class="gi">+++ b/compiler/rustc_macros/src/query.rs</span>
<span class="gu">@@ -273,10 +273,32 @@ fn process_modifiers(query: &amp;mut Query) -&gt; QueryModifiers {</span>
                     panic!("duplicate modifier `desc` for query `{}`", query.name);
                 }
                 if query.doc_comments.is_empty() {
<span class="gd">-                    // FIXME: replace `{}` with `_` somehow</span>
<span class="gd">-                    let format_str = list.first().unwrap();</span>
<span class="gi">+                    use ::syn::*;</span>
<span class="gi">+                    let mut list = list.iter();</span>
<span class="gi">+                    let format_str: String = match list.next() {</span>
<span class="gi">+                        Some(&amp;Expr::Lit(ExprLit { lit: Lit::Str(ref lit_str), .. })) =&gt; {</span>
<span class="gi">+                            lit_str</span>
<span class="gi">+                                .value()</span>
<span class="gi">+                                .replace("`{}`", "{}") // We add them later anyways for consistency</span>
<span class="gi">+                        },</span>
<span class="gi">+                        _ =&gt; panic!("Expected a string literal"),</span>
<span class="gi">+                    };</span>
<span class="gi">+                    let mut fmt_fragments = format_str.split("{}");</span>
<span class="gi">+                    let mut doc_string = fmt_fragments.next().unwrap().to_string();</span>
<span class="gi">+                    list.map(::quote::ToTokens::to_token_stream)</span>
<span class="gi">+                        .zip(fmt_fragments)</span>
<span class="gi">+                        .for_each(|(tts, next_fmt_fragment)| {</span>
<span class="gi">+                            use ::core::fmt::Write;</span>
<span class="gi">+                            write!(</span>
<span class="gi">+                                &amp;mut doc_string,</span>
<span class="gi">+                                " `{}` {}",</span>
<span class="gi">+                                tts.to_string().replace(" . ", "."),</span>
<span class="gi">+                                next_fmt_fragment,</span>
<span class="gi">+                            ).unwrap();</span>
<span class="gi">+                        })</span>
<span class="gi">+                    ;</span>
                     let comment = parse_quote! {
<span class="gd">-                        #[doc = #format_str]</span>
<span class="gi">+                        #[doc = #doc_string]</span>
                     };
                     query.doc_comments.push(comment);
                 }
</code></pre></div>
<p>It needs to be formatted, and feel free to "rewrite" the more hacky / ugly parts <span aria-label="upside down" class="emoji emoji-1f643" role="img" title="upside down">:upside_down:</span></p>



<a name="220889772"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Evaluate%20format%20string%20in%20proc-macro%20%2380337/near/220889772" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro.20.2380337.html#220889772">(Dec 24 2020 at 20:45)</a>:</h4>
<p>I like it. We can micro optimize later</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>